//
//  ViewController.m
//  contact
//
//  Created by lsy on 15/11/21.
//  Copyright © 2015年 刘世玉. All rights reserved.
//

#import "ViewController.h"
#import "Member.h"
#import "LSYcoreDataManager.h"
#import "LSYDetailViewController.h"
#import <Masonry.h>
#import "LSYmemViewController.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic, strong)LSYcoreDataManager *manager;
@property(nonatomic, strong)UITableView *table;
@property(nonatomic, strong)NSMutableArray *dataArr;
@property(nonatomic, strong)UILabel *label;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self getData];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(goAdd)];
   
    self.table = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.table.dataSource = self;
    self.table.delegate = self;
    [self.table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
    [self.view addSubview:self.table];
    
    self.label = [[UILabel alloc] init];
    [self.view addSubview:self.label];
    
    
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view);
        make.size.equalTo(self.view);
        
        
    }];
    self.label.textAlignment = 1;
    self.label.textColor = [UIColor grayColor];
    self.label.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(goAdd)];
    [self.label addGestureRecognizer:tap];
    
    self.label.text = @"当前通讯录为空，请进行添加";

    // Do any additional setup after loading the view, typically from a nib.
} 

- (void)viewWillAppear:(BOOL)animated
{
    [self getData];
    [self.table reloadData];
}

- (void)goAdd
{
    LSYDetailViewController *detailVC =[[LSYDetailViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.dataArr.count > 0) {
        self.label.hidden = YES;
    }else{
        self.label.hidden = NO;
    }
    return self.dataArr.count;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    cell.textLabel.text = [self.dataArr[indexPath.row] name];
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@",[self.dataArr[indexPath.row] number]];
    if ([[self.dataArr[indexPath.row] sex] isEqualToString:@"male"]) {
        cell.imageView.image = [UIImage imageNamed:@"male.png"];
    }else if ([[self.dataArr[indexPath.row] sex] isEqualToString:@"female"])
    {
        cell.imageView.image = [UIImage imageNamed:@"female.png"];
    }
    else{
        cell.imageView.image = [UIImage imageNamed:@"question.png"];

    }
        return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LSYmemViewController *detailVC =[[LSYmemViewController alloc] init];
    [self.navigationController pushViewController:detailVC animated:YES];
    detailVC.name.text = [self.dataArr[indexPath.row] name];
    detailVC.sex.text = [self.dataArr[indexPath.row] sex];
    detailVC.number.text = [NSString stringWithFormat:@"%@",[self.dataArr[indexPath.row] number]];
    detailVC.phoneNum.text =[NSString stringWithFormat:@"%@",[self.dataArr[indexPath.row] phoneNum]];
    detailVC.qq.text = [NSString stringWithFormat:@"%@",[self.dataArr[indexPath.row] qq]];
    detailVC.birthPlace.text = [self.dataArr[indexPath.row] birthPlace];
}

- (void)getData
{
    self.manager = [LSYcoreDataManager defaults];
    //获取查询数据的请求， 相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Member"];
//    //添加约束  谓词
//    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@" ], @"成精" ];
//    request.predicate = predicate;
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"firstWord" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    self.dataArr = [NSMutableArray arrayWithArray:array];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
